﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LabMS.Web.Equipment.LowValue
{
    public partial class lowdamagereport : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("DZPBS");//必须写在ispostback外
            strPageUserType.Add("mana");

            tbReportTime.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this);");
            if (!IsPostBack)
            {
                dllbind();
                tbReportTime.Attributes.Add("readOnly", "-1");
                tbInstrumentName.Attributes.Add("readOnly", "-1");
                tbInstrumentNum.Attributes.Add("readOnly", "-1");
                tbReportTime.Attributes.Add("readOnly", "-1");
                tbApplyer.Attributes.Add("readOnly", "-1");
            }
        }

        protected void dllbind()
        {
            tbReportTime.Text = DateTime.Now.ToShortDateString();
            LabMS.BLL.Lab blab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> mLab = new List<LabMS.Model.Lab>();
            mLab = blab.GetModelList(" Lab_ParentID is null ");
            ddlLab.DataSource = mLab;
            ddlLab.DataTextField = "Lab_Name";
            ddlLab.DataValueField = "Lab_Code";
            ddlLab.DataBind();


        }


        protected void savedata(string status)
        {
            if (tbInstrumentName.Text.Trim() == "")
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "仪器不可以为空!");
                return;
            }
            if (tbApplyer.Text.Trim() == "")
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "申请人不可以为空!");
                return;
            }
            BLL.GoodsMaintain binstrument = new LabMS.BLL.GoodsMaintain();
            Model.GoodsMaintain instrment = new LabMS.Model.GoodsMaintain();
            instrment.Numbers = tbInstrumentNum.Text;
            instrment.Name = tbInstrumentName.Text;
            instrment.AssetManagers = tbManager.Text.Trim();
            instrment.Tel = tbTelNum.Text;
            instrment.StorageSites = tbLocation.Text; //ddlStockLocation.SelectedValue;
            instrment.LabName = ddlLab.SelectedValue;
            instrment.Guarantee = isInTime.SelectedValue;
            instrment.Applicant = tbApplyer.Text.Trim();
            instrment.ReportedTime = DateTime.Parse( tbReportTime.Text);
            instrment.Describe = tbDescription.Text;
            instrment.Status = status.ToString().Trim();
            BLL.InstrumentAndConsumables bins = new LabMS.BLL.InstrumentAndConsumables();
            Model.InstrumentAndConsumables ins = (bins.GetModelList(" Numbers="+tbInstrumentNum.Text.Trim()))[0];
            ins.PresentSituation = status;
            int i = Convert.ToInt32(ins.Quantity);
            i--;
            ins.Quantity = i;
            try
            {
                bins.Update(ins);
            }
            catch (Exception e)
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "添加失败1");
                return;
            }
            try
            {
                binstrument.Add(instrment);
            }
            catch (Exception ee)
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "添加失败2"+ee.Message);
                return;
            }
            LabMS.Common.JShelper.JSAlertAndRedirect(Page, "err", "添加成功", "lowdamagereport.aspx");
        }


        //报损
        protected void lbReportDamage_Click(object sender, EventArgs e)
        {
            savedata("故障");
        }
        //报费
        protected void lbReportDead_Click(object sender, EventArgs e)
        {
            savedata("报废");
        }
        //报失
        protected void lbReportLost_Click(object sender, EventArgs e)
        {
            savedata("丢失");
        }
    }
}
